package com.avic.rong.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.WindowManager;
import android.widget.ImageView;

import com.avic.rong.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.minato.minastore.utils.GlideApp;

import java.io.File;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by Administrator on 2017/6/2 0002.
 */

public class GlideUtils {


    static int imageWidthPixels = 1024;
    static int imageHeightPixels = 768;
    private static int CONOR = 10;

    /**
     * @param context 上下文
     * @param url     图片加载地址
     * @param iv      显示图片的ImageView
     */
    public static void loaderGifORImage(final Context context, final String url, final ImageView iv) {
//    public static void loaderImage(Context context, String url, ImageView iv) {
        if (context != null) {

            final RequestOptions options = new RequestOptions()
//                    .centerCrop()
                    .placeholder(R.mipmap.doc_default)
                    .dontAnimate()
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
//                    .override(imageWidthPixels,imageHeightPixels)
                    .skipMemoryCache(true)
                    .priority(Priority.NORMAL);


            GlideApp.with(context).asFile().load(url).into(new SimpleTarget<File>() {
                @Override
                public void onResourceReady(@NonNull File resource, @Nullable Transition<? super File> transition) {

                    String imageType = BitmapUtils.getImageType(resource);

                    if ("gif".equals(imageType)) {

                        GlideApp.with(context).asGif().load(url).apply(options).into(iv);
                    } else {
//                        Glide.with(context).load(url).placeholder(R.mipmap.common_default_bg).into(iv);
                        GlideApp.with(context).load(url).apply(options).into(iv);
                    }

                }
            });
        }
    }


    /**
     * @param context 上下文
     * @param url     图片加载地址
     * @param iv      显示图片的ImageView
     */
    public static void loaderImageWithBaoliao(Context context, String url, ImageView iv) {
        if (context != null) {

            RequestOptions options = new RequestOptions()
//                    .centerCrop()
                    .placeholder(R.mipmap.doc_default)
                    .dontAnimate()
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .priority(Priority.NORMAL);

//            Glide.with(context).load(url).apply(options).into(iv);

            WindowManager wm = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            int SCREEN_WIDTH = wm.getDefaultDisplay().getWidth();
            int height = wm.getDefaultDisplay().getHeight();
            GlideApp.with(context).load(url + "?imageView2/4/w/" + SCREEN_WIDTH * 2).apply(options).into(iv);
        }
    }


    /**
     * @param context 上下文
     * @param url     图片加载地址
     * @param iv      显示图片的ImageView
     */
    public static void loaderImage(Context context, String url, ImageView iv) {
        if (context != null) {

            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.doc_default)
//                    .dontAnimate()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .priority(Priority.NORMAL);

            GlideApp.with(context).load(url).apply(options).into(iv);

        }
    }

    /**
     * @param context 上下文
     * @param url     图片加载地址
     * @param iv      显示图片的ImageView
     */
    public static void loaderImageDefaultScaleType(Context context, String url, ImageView iv) {
        if (context != null) {

            RequestOptions options = new RequestOptions()
//                    .centerCrop()
                    .placeholder(R.drawable.pager_default_bg)
                    .error(R.drawable.pager_default_bg)
                    .dontAnimate()
                    .skipMemoryCache(true)
                    .priority(Priority.NORMAL);

            GlideApp.with(context).load(url).apply(options).into(iv);
        }
    }

    /**
     * 圆角默认图片
     */
    public static void loaderRoundImage(Context context, String url, ImageView iv) {
        if (context != null) {
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.doc_default)
                    .error(R.mipmap.doc_default)
                    .dontAnimate()
                    .skipMemoryCache(true)
                    .priority(Priority.NORMAL);

            GlideApp.with(context).load(url).apply(options).into(iv);
        }
    }


    /**
     * 加载网络图片 处理成圆角 centerCrop()方法的源码可知,也是需要调用transform()方法的.所以前后共用CenterCrop会覆盖掉GlideRoundImage的效果
     * ---处理方法2个效果同时放在transform里面
     */
    public static void loaderHanldeRoundImage(final Context context, String url, final ImageView iv, int Conor) {
        if (context != null) {

            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.doc_default)
                    .error(R.mipmap.doc_default)
                    .dontAnimate()
                    .skipMemoryCache(true)
                    //.transforms(new CenterCrop(), new GlideRoundTransform(context, Conor))
                    .transform(new CenterCrop(),new RoundedCorners(Conor))
                    .priority(Priority.NORMAL);


            GlideApp.with(context).load(url).apply(options).
                    into(iv);
        }
    }

    /**
     * 加载网络图片 处理成圆角 centerCrop()方法的源码可知,也是需要调用transform()方法的.所以前后共用CenterCrop会覆盖掉GlideRoundImage的效果
     * ---处理方法2个效果同时放在transform里面
     */
    public static void loaderHanldeRoundImageResource(final Context context, int url, final ImageView iv, int Conor) {
        if (context != null) {

            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.doc_default)
                    .error(R.mipmap.doc_default)
                    .dontAnimate()
                    .skipMemoryCache(true)
                    //.transforms(new CenterCrop(), new GlideRoundTransform(context, Conor))
                    .transform(new CenterCrop(),new RoundedCorners(Conor))
                    .priority(Priority.NORMAL);


            GlideApp.with(context).load(url).apply(options).
                    into(iv);
        }
    }

    /**
     * 加载网络图片 处理成圆角 centerCrop()方法的源码可知,也是需要调用transform()方法的.所以前后共用CenterCrop会覆盖掉GlideRoundImage的效果
     * ---处理方法2个效果同时放在transform里面
     */
    public static void loaderHanldeRoundImage(final Context context, String url, final ImageView iv,int resId,int radius) {
        if (context != null) {

            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(resId)
                    .error(resId)
                    .skipMemoryCache(false)
                    .transform(new CenterCrop(),new RoundedCorners(radius))
                    .priority(Priority.NORMAL);


            GlideApp.with(context).load(url).apply(options).
                    into(iv);

//            GlideApp.with(context).load("http://zjggbdstold.zjgonline.com.cn/e371e976-37a0-4e2e-a058-ec1ef73733f3").
//                    into(iv);
        }
    }

    public static void loaderHanldeRoundImage(final Context context, String url, final ImageView iv, Drawable drawable, int radius) {
        if (context != null) {

            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(drawable)
                    .error(drawable)
                    .skipMemoryCache(false)
                    .dontAnimate()
                    .transform(new CenterCrop(),new RoundedCorners(radius))
                    .priority(Priority.NORMAL);


            GlideApp.with(context).load(url).apply(options).
                    into(iv);

//            GlideApp.with(context).load("http://zjggbdstold.zjgonline.com.cn/e371e976-37a0-4e2e-a058-ec1ef73733f3").
//                    into(iv);
        }
    }

    /**
     * 加载网络图片 处理成圆角 centerCrop()方法的源码可知,也是需要调用transform()方法的.所以前后共用CenterCrop会覆盖掉GlideRoundImage的效果
     * ---指定圆角
     */
    public static void loaderRoundImageLeftTB(final Context context, String url, final ImageView iv, int corners) {
        if (context != null) {

            //GlideRoundTransform glideRoundTransform = new GlideRoundTransform(context, corners, GlideRoundTransform.CORNER_TOP_LEFT | GlideRoundTransform.CORNER_BOTTOM_LEFT);
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.doc_default)
                    .error(R.mipmap.doc_default)
                    .dontAnimate()
                    .skipMemoryCache(true)
                    //.transforms(new CenterCrop(), glideRoundTransform)
                    .transform(new RoundedCorners(corners))
                    .priority(Priority.NORMAL);


            GlideApp.with(context).load(url).apply(options).into(iv);
        }
    }

    public static void loaderRoundImageLeftRightT(final Context context, String url, final ImageView iv, int corners) {
        if (context != null) {
//            GlideRoundTransform glideRoundTransform = new GlideRoundTransform(context, corners, GlideRoundTransform.CORNER_TOP_LEFT | GlideRoundTransform.CORNER_TOP_RIGHT);
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.doc_default)
                    .error(R.mipmap.doc_default)
                    .dontAnimate()
                    .skipMemoryCache(true)
                    //.transforms(new CenterCrop(), glideRoundTransform)
                    .transform(new RoundedCorners(corners))
                    .priority(Priority.NORMAL);

            GlideApp.with(context).load(url).apply(options)
                    .into(iv);
        }
    }


    /**
     * 圆角边框加默认图片
     */
    public static void loaderGlideCircleImage(Context context, String url, ImageView iv) {
        if (context != null) {
            GlideApp.with(context).load(url).skipMemoryCache(true)
                    .error(R.mipmap.ic_launcher)
                    .placeholder(R.mipmap.ic_launcher).transform(new GlideCircleTransform(context)).into(iv);
        }
    }
    /**
     * 圆角边框加默认图片
     */
    public static void loaderGlideCircleImageIC(Context context, String url, ImageView iv) {
        if (context != null) {
            GlideApp.with(context).load(url).placeholder(R.mipmap.icon_default_bg_szh)
                    .error(R.mipmap.icon_default_bg_szh)
                    .skipMemoryCache(true)
                    .transform(new GlideCircleTransform(context))
                    .into(iv);
        }
    }
    /**
     * 圆角边框加默认图片
     */
    public static void loaderGlideREDCircleImage(Context context, String url, ImageView iv) {
        if (context != null) {
            GlideApp.with(context).load(url) .placeholder(R.mipmap.icon_default_bg_szh)
                    .error(R.mipmap.icon_default_bg_szh).transform(new GlideCircleRedTransform(context)).into(iv);
        }
    }


    public static void loaderImage4_3_fitxy(Context context, String url, ImageView iv) {
        if (context != null) {
            GlideApp.with(context).load(url).skipMemoryCache(true).error(R.mipmap.doc_default).
                    placeholder(R.mipmap.doc_default).dontAnimate().into(iv);
        }
    }


//
//    public static void loadRoundImage(final Context context, final int cornerRadius, String url,int resId,final ImageView imageView){
//        GlideApp.with(context)
//                .load(url)
//                .asBitmap()
//                .placeholder(resId)
//                .diskCacheStrategy(DiskCacheStrategy.ALL) //设置缓存
//                .into(new BitmapImageViewTarget(imageView){
//                    @Override
//                    protected void setResource(Bitmap resource) {
//                        super.setResource(resource);
//                        RoundedBitmapDrawable circularBitmapDrawable =
//                                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
//                        circularBitmapDrawable.setCornerRadius(cornerRadius); //设置圆角弧度
//                        imageView.setImageDrawable(circularBitmapDrawable);
//                    }
//                });
//    }


    public static void loadRoundImage(Context context, int cornerRadius, String url,int resId,final ImageView imageView) {
//        GlideApp.with(context)
//                .load(url)
//                .asBitmap()
//                .placeholder(resId)
//                .diskCacheStrategy(DiskCacheStrategy.ALL) //设置缓存
//                .into(new BitmapImageViewTarget(imageView){
//                    @Override
//                    protected void setResource(Bitmap resource) {
//                        super.setResource(resource);
//                        RoundedBitmapDrawable circularBitmapDrawable =
//                                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
//                        circularBitmapDrawable.setCornerRadius(cornerRadius); //设置圆角弧度
//                        imageView.setImageDrawable(circularBitmapDrawable);
//                    }
//                });
        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(cornerRadius);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(DensityUtils.dp2px(context,115),DensityUtils.dp2px(context,115));

        RoundedCornersTransformation roundedCornersTransformation = new RoundedCornersTransformation(20, 0, RoundedCornersTransformation.CornerType.ALL);

        Glide.with(context).load(url)
                .apply(RequestOptions.bitmapTransform(roundedCornersTransformation))
                .placeholder(resId)
                .into(imageView);

//        Glide.with(context).load(url).apply(options).listener(new RequestListener<Drawable>() {
//            @Override
//            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//                return false;
//            }
//
//            @Override
//            public boolean onResourceReady(Drawable drawable, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                imageView.setBackground(drawable);
//                return false;
//            }
//        }).submit();
    }



}
